
package com.jf.cloud.product.controller.multishop;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.product.dto.StockChangeReasonDTO;
import com.jf.cloud.product.model.StockChangeReason;
import com.jf.cloud.product.model.StockChangeReasonLang;
import com.jf.cloud.product.service.StockChangeReasonLangService;
import com.jf.cloud.product.service.StockChangeReasonService;
import com.jf.cloud.product.vo.StockChangeReasonVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 出入库原因
 *
 * @author zz
 * @date 2022-06-22 17:14:22
 */
@RestController("multishopStockChangeReasonController")
@RequestMapping("/m/stock_change_reason")
@Tag(name = "出入库原因")
public class StockChangeReasonController {

    @Autowired
    private StockChangeReasonService stockChangeReasonService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private StockChangeReasonLangService stockChangeReasonLangService;

    @GetMapping("/page")
    @Operation(summary = "分页获取出入库原因列表" , description = "分页获取出入库原因列表")
    public ServerResponseEntity<PageVO<StockChangeReasonVO>> page(@Valid PageDTO pageDTO, @ParameterObject StockChangeReasonDTO stockChangeReasonDTO) {
        stockChangeReasonDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<StockChangeReasonVO> stockChangeReasonPage = stockChangeReasonService.page( pageDTO, stockChangeReasonDTO);
        return ServerResponseEntity.success(stockChangeReasonPage);
    }

    @GetMapping("/list")
    @Operation(summary = "获取店铺所有的出入库原因" , description = "获取店铺所有的出入库原因")
    public ServerResponseEntity<List<StockChangeReasonVO>> list(StockChangeReasonDTO stockChangeReasonDTO){
        stockChangeReasonDTO.setShopId(AuthUserContext.get().getTenantId());
        List<StockChangeReasonVO> stockChangeReasonPage = stockChangeReasonService.list(stockChangeReasonDTO);
        return ServerResponseEntity.success(stockChangeReasonPage);
    }

    @GetMapping
    @Operation(summary = "获取出入库原因" , description = "根据stockChangeReasonId获取出入库原因")
    public ServerResponseEntity<StockChangeReasonVO> getByStockChangeReasonId(@RequestParam Long stockChangeReasonId) {
        return ServerResponseEntity.success(stockChangeReasonService.getByStockChangeReasonId(stockChangeReasonId));
    }

    @PostMapping
    @Operation(summary = "保存出入库原因" , description = "保存出入库原因")
    public ServerResponseEntity<Void> save(@Valid @RequestBody StockChangeReasonDTO stockChangeReasonDTO) {
        stockChangeReasonService.save(stockChangeReasonDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新出入库原因" , description = "更新出入库原因")
    public ServerResponseEntity<Void> update(@Valid @RequestBody StockChangeReasonDTO stockChangeReasonDTO) {
        stockChangeReasonDTO.setShopId(AuthUserContext.get().getTenantId());
        StockChangeReason stockChangeReason = mapperFacade.map(stockChangeReasonDTO, StockChangeReason.class);
        stockChangeReasonService.update(stockChangeReason);
        //更新出入库原因和备注信息
        stockChangeReasonLangService.update(mapperFacade.map(stockChangeReasonDTO, StockChangeReasonLang.class));
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除出入库原因" , description = "根据出入库原因id删除出入库原因")
    public ServerResponseEntity<Void> delete(@RequestParam Long stockChangeReasonId) {
        stockChangeReasonService.deleteById(stockChangeReasonId);
        return ServerResponseEntity.success();
    }

    @PutMapping("/changeStatus" )
    @Schema(description = "修改出入库原因状态" )
    public ServerResponseEntity<Void> changeStatus(@RequestParam("stockChangeReasonId") Long stockChangeReasonId, @RequestParam("status") Integer status) {
        Long shopId = AuthUserContext.get().getTenantId();
        stockChangeReasonService.changeStatus(shopId, stockChangeReasonId, status);
        return ServerResponseEntity.success();
    }
}
